package jgl.vajra.kingkong.part.mine.activity;

import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.MyWalletBean;
import jgl.vajra.kingkong.bean.MyWalletDetailsBean;
import jgl.vajra.kingkong.databinding.ActivityMyWalletBinding;
import jgl.vajra.kingkong.dialogFragment.RechargeInstructionsDialogFragment;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.manager.IntentManager;
import jgl.vajra.kingkong.part.mine.contract.MyWalletContract;
import jgl.vajra.kingkong.part.mine.viewModel.MyWalletViewModel;
import jgl.vajra.kingkong.utils.RandomUtils;
import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(MyWalletViewModel.class)
public class MyWalletActivity extends BaseMVVMActivity<MyWalletViewModel, ActivityMyWalletBinding> implements MyWalletContract.View, BaseBindingItemPresenter<MyWalletBean.ParameterListBean> {

    private RechargeInstructionsDialogFragment rechargeInstructionsDialogFragment;
    private SingleTypeBindingAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_wallet;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_my_wallet);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        requestNetData();
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        mViewModel.getMyWalletData(map);
    }

    //查看明细
    @Override
    public void onDetailed() {
        IntentManager.toWalletDetailedActivity(this);
    }

    @Override
    public void onConvertedConsumption(String amount, int type) {
        Map<String ,Object> map=new HashMap<>();
        map.put("Authorization",AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("amount",AEACCoder.encrypt(amount + "&&" + RandomUtils.getRandom()));
        map.put("tranferType",type);

        mViewModel.getConvertedConsumptionData(map);
    }

    //提现
    @Override
    public void onWithdrawal(String amount, int type) {
        IntentManager.toWithdrawalPageActivity(this, amount, type);
    }

    //充电须知
    @Override
    public void onRechargeInstructions() {
        if (rechargeInstructionsDialogFragment == null)
            rechargeInstructionsDialogFragment = new RechargeInstructionsDialogFragment();
        rechargeInstructionsDialogFragment.show(this.getSupportFragmentManager());
    }

    @Override
    public void returnMyWalletData(MyWalletBean data) {
        mBinding.tvBalance.setText(AEACCoder.decrypt(data.sumAmount));

        MyWalletDetailsBean bean = new MyWalletDetailsBean();
        for (int i = 0; i < data.data.size(); i++) {
            switch (i) {
                case 0:
                    bean.consumptionName = data.data.get(i).name;
                    bean.consumptionAmount = data.data.get(i).amount;
                    bean.consumptionType = data.data.get(i).type;
                    break;
                case 1:
                    bean.giveName = data.data.get(i).name;
                    bean.giveAmount = data.data.get(i).amount;
                    bean.giveType = data.data.get(i).type;
                    break;
                case 2:
                    bean.shareProfitName = data.data.get(i).name;
                    bean.shareProfitAmount = data.data.get(i).amount;
                    bean.shareProfitType = data.data.get(i).type;
                    bean.shareProfitTranferType = data.data.get(i).tranferType;
                    break;
                case 3:
                    bean.teamProfitName = data.data.get(i).name;
                    bean.teamProfitAmount = data.data.get(i).amount;
                    bean.teamProfitType = data.data.get(i).type;
                    bean.teamProfitTranferType = data.data.get(i).tranferType;
                    break;
            }
        }
        mBinding.setData(bean);
        adapter.refresh(data.parameter);

    }

    @Override
    public void returnConvertedConsumptionData(String data) {
        requestNetData();
    }

    @Override
    public void onItemClick(int position, MyWalletBean.ParameterListBean itemData) {
        IntentManager.toRechargeActivity(this, itemData.amount+"", itemData.id);
    }
}
